<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>自媒体广告配置管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {

			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
            $('select').select2();
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
		function findProvince(v) {
                $.ajax({
                    type : "post",
                    async : false,
                    url : "${ctx}/sys/area/getAreaListByParentId",
                    data : {
                        'pid' : v
                    },
                    dataType : "json",
                    success : function(msg) {
                        $("#province").empty();
                        $("#city").empty();
                        $("#province").select2();
                        var $option2 = $("<option>").attr({
                            "value" :""
                        }).text("全部");
                        $("#province").append($option2);
                        if (msg.length > 0) {
                            for (var i = 0; i < msg.length; i++) {
                                var partId = msg[i].id;
                                var partName = msg[i].name;
                                var $option = $("<option>").attr({
                                    "value" : partId
                                }).text(partName);
                                $("#province").append($option);
                            }

                            $("#province").change();

                        }
                    },
                    error : function(json) {
                        $.jBox.alert("网络异常！");
                    }
                });
            }
            function findCity(v) {
            $.ajax({
                type : "post",
                async : false,
                url : "${ctx}/sys/area/getAreaListByParentId",
                data : {
                    'pid' : v
                },
                dataType : "json",
                success : function(msg) {

                    $("#city").empty();
                    $("#city").select2();
                    var $option2 = $("<option>").attr({
                        "value" :""
                    }).text("全部");
                    $("#city").append($option2);
                    if (msg.length > 0) {
                        for (var i = 0; i < msg.length; i++) {
                            var partId = msg[i].id;
                            var partName = msg[i].name;
                            var $option = $("<option>").attr({
                                "value" : partId
                            }).text(partName);
                            $("#city").append($option);
                        }

                        $("#city").change();

                    }
                },
                error : function(json) {
                    $.jBox.alert("网络异常！");
                }
            });
        }
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/adv/hkMediaAdvReward/">自媒体广告配置列表</a></li>
		<li class="active"><a href="${ctx}/adv/hkMediaAdvReward/form?id=${hkMediaAdvReward.id}">自媒体广告配置<shiro:hasPermission name="adv:hkMediaAdvReward:edit">${not empty hkMediaAdvReward.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="adv:hkMediaAdvReward:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="hkMediaAdvReward" action="${ctx}/adv/hkMediaAdvReward/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<form:hidden path="media.id"/>

		<div class="control-group">
			<label class="control-label">开始时间：</label>
			<div class="controls">
				<input name="beginDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
					value="<fmt:formatDate value="${hkMediaAdvReward.beginDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">结束时间：</label>
			<div class="controls">
				<input name="endDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate "
					value="<fmt:formatDate value="${hkMediaAdvReward.endDate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">国家：</label>
			<div class="controls">
				<form:select path="country.id" id="country" class="input-medium required" onchange="findProvince(this.options[this.options.selectedIndex].value);">
					<form:option value="" label=""/>
					<form:options items="${countrys}" itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>

				<%--<sys:treeselect id="country" name="country.id" value="${hkMediaAdvReward.country.id}" labelName="country.name" labelValue="${hkMediaAdvReward.country.name}"
					title="区域" url="/sys/area/treeData?type=1" cssClass="" allowClear="true" notAllowSelectParent="false"/>--%>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">省：</label>
			<div class="controls">
				<form:select path="province.id" id="province" class="input-medium" onchange="findCity(this.options[this.options.selectedIndex].value);">
					<form:option value="" label="全部"/>
					<form:options items="${provinces}" itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
				<%--<sys:treeselect id="province" name="province.id" value="${hkMediaAdvReward.province.id}" labelName="province.name" labelValue="${hkMediaAdvReward.province.name}"--%>
					<%--title="区域" url="/sys/area/treeData?type=2" cssClass="" allowClear="true" notAllowSelectParent="false"/>true--%>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">市：</label>
			<div class="controls">
				<form:select path="city.id" id="city" class="input-medium">
					<form:option value="" label="全部"/>
					<form:options items="${citys}" itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
				<%--<sys:treeselect id="city" name="city.id" value="${hkMediaAdvReward.city.id}" labelName="city.name" labelValue="${hkMediaAdvReward.city.name}"
					title="区域" url="/sys/area/treeData?type=3" cssClass="" allowClear="true" notAllowSelectParent="false"/>--%>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="100" class="input-xxlarge "/>
			</div>
		</div>
			<div class="control-group">
				<label class="control-label">自媒体广告物品：</label>
				<div class="controls">
					<table id="contentTableGoods" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>物品</th>
								<th>数量</th>
								<th>备注</th>
								<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="hkMediaAdvGoodsList">
						</tbody>
						<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><tfoot>
							<tr><td colspan="10"><a href="javascript:" onclick="addRow('#hkMediaAdvGoodsList', hkMediaAdvGoodsRowIdx, hkMediaAdvGoodsTpl);hkMediaAdvGoodsRowIdx = hkMediaAdvGoodsRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="hkMediaAdvGoodsTpl">//<!--
						<tr id="hkMediaAdvGoodsList{{idx}}">
							<td class="hide">
								<input id="hkMediaAdvGoodsList{{idx}}_id" name="hkMediaAdvGoodsList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="hkMediaAdvGoodsList{{idx}}_delFlag" name="hkMediaAdvGoodsList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
							 	<select id="hkMediaAdvGoodsList{{idx}}_goods" name="hkMediaAdvGoodsList[{{idx}}].goods.id" data-value="{{row.goods.id}}" class="input-small ">
									<option value=""></option>
									<c:forEach items="${goods}" var="dict">
										<option value="${dict.id}">${dict.name}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="hkMediaAdvGoodsList{{idx}}_num" name="hkMediaAdvGoodsList[{{idx}}].num" type="text" value="{{row.num}}" maxlength="11" class="input-small "/>
							</td>


							<td>
								<textarea id="hkMediaAdvGoodsList{{idx}}_remarks" name="hkMediaAdvGoodsList[{{idx}}].remarks" rows="2" maxlength="100" class="input-small ">{{row.remarks}}</textarea>
							</td>
							<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#hkMediaAdvGoodsList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var hkMediaAdvGoodsRowIdx = 0, hkMediaAdvGoodsTpl = $("#hkMediaAdvGoodsTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(hkMediaAdvReward.hkMediaAdvGoodsList)};
							for (var i=0; i<data.length; i++){
								addRow('#hkMediaAdvGoodsList', hkMediaAdvGoodsRowIdx, hkMediaAdvGoodsTpl, data[i]);
								hkMediaAdvGoodsRowIdx = hkMediaAdvGoodsRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">自媒体广告积分：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>

								<th>积分类型</th>
								<th>积分</th>

								<th>备注</th>
								<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
							</tr>
						</thead>
						<tbody id="hkMediaAdvIntegralList">
						</tbody>
						<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><tfoot>
							<tr><td colspan="10"><a href="javascript:" onclick="addRow('#hkMediaAdvIntegralList', hkMediaAdvIntegralRowIdx, hkMediaAdvIntegralTpl);hkMediaAdvIntegralRowIdx = hkMediaAdvIntegralRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="hkMediaAdvIntegralTpl">//<!--
						<tr id="hkMediaAdvIntegralList{{idx}}">
							<td class="hide">
								<input id="hkMediaAdvIntegralList{{idx}}_id" name="hkMediaAdvIntegralList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="hkMediaAdvIntegralList{{idx}}_delFlag" name="hkMediaAdvIntegralList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>

							<td>
								<select id="hkMediaAdvIntegralList{{idx}}_type" name="hkMediaAdvIntegralList[{{idx}}].type" data-value="{{row.type}}" class="input-small ">
									<option value=""></option>
									<c:forEach items="${fns:getDictList('hk_adv_integral_type')}" var="dict">
										<option value="${dict.value}">${dict.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="hkMediaAdvIntegralList{{idx}}_integral" name="hkMediaAdvIntegralList[{{idx}}].integral" type="text" value="{{row.integral}}" maxlength="11" class="input-small "/>
							</td>

							<td>
								<textarea id="hkMediaAdvIntegralList{{idx}}_remarks" name="hkMediaAdvIntegralList[{{idx}}].remarks" rows="2" maxlength="100" class="input-small ">{{row.remarks}}</textarea>
							</td>
							<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#hkMediaAdvIntegralList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var hkMediaAdvIntegralRowIdx = 0, hkMediaAdvIntegralTpl = $("#hkMediaAdvIntegralTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(hkMediaAdvReward.hkMediaAdvIntegralList)};
							for (var i=0; i<data.length; i++){
								addRow('#hkMediaAdvIntegralList', hkMediaAdvIntegralRowIdx, hkMediaAdvIntegralTpl, data[i]);
								hkMediaAdvIntegralRowIdx = hkMediaAdvIntegralRowIdx + 1;
							}
                            $('select').select2();
						});
					</script>
				</div>
			</div>
		<div class="form-actions">
			<shiro:hasPermission name="adv:hkMediaAdvReward:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>